package com.zzyl.serve.service.impl.admin;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.admin.NursingLevelMapper;
import com.zzyl.serve.domain.admin.NursingLevel;
import com.zzyl.serve.service.admin.INursingLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;

/**
 * 护理等级Service业务层处理
 *
 * @author howstrong
 * @date 2024-10-04
 */
@Service
@Slf4j
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService {
    /*定义一个常量用来表Redis的键*/
    public static final String CACHE_NURSINGLEVEL_KEY = "nursingLevel:all";
    @Autowired
    private NursingLevelMapper nursingLevelMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询护理等级
     *
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id) {
        return getById(id);
    }

    /**
     * 查询护理等级列表
     *
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevel> selectNursingLevelList(NursingLevel nursingLevel) {
        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel) {


        int result = save(nursingLevel) ? 1 : 0;
        /*如果新增成功就去删除缓存,原理就是根据键去删除*/
        if (result == 1) {
            redisTemplate.delete(CACHE_NURSINGLEVEL_KEY);
        }
        return result;
    }

    /**
     * 修改护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel) {
        int result = updateById(nursingLevel) ? 1 : 0;
        /*如果修改成功就去删除缓存,原理就是根据键去删除*/
        if (result == 1) {
            redisTemplate.delete(CACHE_NURSINGLEVEL_KEY);
        }
        return result;
    }

    /**
     * 批量删除护理等级
     *
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids) {
        int result = removeByIds(Arrays.asList(ids)) ? 1 : 0;
        /*如果删除成功就去删除缓存,原理就是根据键去删除*/
        if (result == 1) {
            redisTemplate.delete(CACHE_NURSINGLEVEL_KEY);
        }
        return result;
    }

    /**
     * 删除护理等级信息
     *
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id) {
        int result = removeById(id) ? 1 : 0;
        /*如果删除成功就去删除缓存,原理就是根据键去删除*/
        if (result == 1) {
            redisTemplate.delete(CACHE_NURSINGLEVEL_KEY);
        }
        return result;
    }

    /*查询全部护理等级*/
    @Override
    public List<NursingLevel> all() {
        /*先去根据常量key去拿数据*/
        /*因为我们知道肯定是一个list所以直接加泛型*/
        List<NursingLevel> list = (List<NursingLevel>) redisTemplate.opsForValue().get(CACHE_NURSINGLEVEL_KEY);
        /*使用工具类去判断是否为空*/
        if (ObjectUtils.isNotEmpty(list)) {
            /*不为空就返回数据*/
            log.info("----redis------从Redis里面拿数据-------------");
            return list;
        }
        /*为空的话就去数据库里面查并且放到缓存里面*/
        /*依旧去使用Mybatisplus*/
        /*建一个条件构造器*/
        LambdaQueryWrapper<NursingLevel> lqw = new LambdaQueryWrapper<>();
        lqw.eq(NursingLevel::getStatus, 1);
        /*使用本类的方法不需要去加别的*/
        list = list(lqw);
        redisTemplate.opsForValue().set(CACHE_NURSINGLEVEL_KEY, list);

        return list;
    }
}
